import java.util.List;

public class Admin extends User {
    public Admin(String id, String username, String password, String phone) {
        super(id, username, password, phone, "管理员");
    }

    @Override
    public boolean login(String password) {
        if (locked) {
            System.out.println("账户已锁定");
            return false;
        }
        // 直接比对加密后的密码
        if (this.password.equals(password)) {
            loginAttempts = 0;
            return true;
        } else {
            loginAttempts++;
            if (loginAttempts >= 5) {
                locked = true;
                System.out.println("密码错误次数过多，账户已锁定");
            }
            return false;
        }
    }

    public void resetPassword(User user) {
        // 重置密码为"123456"并加密
        user.password = MD5Util.encrypt("123456");
        Log.record("重置密码", "重置用户[" + user.username + "]密码", this.username);
    }

    public void addUser(User user, List<User> users) {
        for (User u : users) {
            if (u.getId().equals(user.getId()) || u.getUsername().equals(user.getUsername())) {
                throw new IllegalArgumentException("用户ID或用户名已存在");
            }
        }
        users.add(user);
        Log.record("添加用户", "添加用户[" + user.username + "]", this.username);
    }

    public void deleteUser(String userId, List<User> users) {
        if (userId.equals("admin001")) {
            throw new IllegalArgumentException("不能删除初始管理员账户");
        }
        User user = users.stream().filter(u -> u.getId().equals(userId)).findFirst().orElse(null);
        if (user != null) {
            users.remove(user);
            Log.record("删除用户", "删除用户[" + user.username + "]", this.username);
        } else {
            throw new IllegalArgumentException("用户不存在");
        }
    }
}